﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using DeliveryPlus.Web.Classes.Utils;
using DeliveryPlus.Web.Controllers;

namespace DeliveryPlus.Web.Areas.ControlPanel.Controllers
{
    /// <summary>
    /// Корневой контроллер панели управления
    /// </summary>
    public class RootController : BaseController
    {
        /// <summary>
        /// Отображает страницу для входа
        /// </summary>
        /// <returns></returns>
        public ActionResult Login()
        {
            return View();
        }

        /// <summary>
        /// Обрабатывает логин в панель управления
        /// </summary>
        /// <param name="password">Пароль</param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Login(string password)
        {
            var ex = ConfigurationManager.AppSettings["SecurityPassword"];
            if (password == ex)
            {
                Response.Cookies.Add(new HttpCookie("admin",PasswordUtils.GenerateMD5PasswordHash(ex))
                {
                    Expires = DateTime.Now.AddDays(60)
                });
                return Redirect("/ControlPanel/");
            }

            return RedirectToAction("Login");
        }

    }
}
